package tr.edu.boun.phys.springmassmodel;

public class Spring {
	private Mass m1;
	private Mass m2;
	private double initialLength;
	private double k; //Spring Constant
	
	public Spring(Mass m1, Mass m2) {
		this.m1 = m1;
		this.m2 = m2;
		this.k = 0.00002;
		
		double dx = m1.getLocation().x - m2.getLocation().x;
		double dy = m1.getLocation().y - m2.getLocation().y;
		this.initialLength = Math.sqrt(dx*dx + dy*dy);
	}
	
	public Mass getMass1() {
		return m1;
	}
	
	public Mass getMass2() {
		return m2;
	}
	
	public double getK() {
		return k;
	}
	
	public void setK(double k) {
		this.k = k;
	}
	
	public Vector2D getForce(Mass m) {
		if(m == m1) {
			return getForceM1();
		} else if(m == m2) {
			return getForceM2();
		} else {
			return new Vector2D(0.0, 0.0);
		}
	}
	
	public Vector2D getForceM1() {
		double dx = m1.getLocation().x - m2.getLocation().x;
		double dy = m1.getLocation().y - m2.getLocation().y;
		double length = Math.sqrt(dx*dx + dy*dy);		
		double deltaLength =  length - initialLength;
		double f = - k * deltaLength;
		
		double fx = f * dx/length;
		double fy = f * dy/length;
		
		return new Vector2D(fx, fy); 
	}
	
	public Vector2D getForceM2() {
		double dx = m2.getLocation().x - m1.getLocation().x;
		double dy = m2.getLocation().y - m1.getLocation().y;
		double length = Math.sqrt(dx*dx + dy*dy);		
		double deltaLength =  length - initialLength;
		double f = - k * deltaLength;
		
		double fx = f * dx/length;
		double fy = f * dy/length;
		
		return new Vector2D(fx, fy); 
	}

	public double getInitialLength() {
		return initialLength;
	}

	public void setInitialLength(double length) {
		initialLength = length;
	}
	
	
}